package com.oracle.dao;

import com.alipay.api.domain.OrderItem;
import com.oracle.dao.impl.OrderItemDao;
import com.oracle.mapper.OrderitemMapper;
import com.oracle.pojo.Orderitem;
import com.oracle.pojo.OrderitemExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.List;

@Repository
public class OrderItemDaoImpl implements OrderItemDao {

    @Autowired
    private OrderitemMapper orderitemMapper;

    @Override
    public int deleteOrderItemByBid(Integer bid) {
        OrderitemExample orderitemExample = new OrderitemExample();
        OrderitemExample.Criteria criteria = orderitemExample.createCriteria();
        criteria.andBidEqualTo(bid);
        int code = orderitemMapper.deleteByExample(orderitemExample);
        if (code>0){
            return 200;
        }
        return 400;
    }

    @Override
    public List<Orderitem> findOrderItemByOid(Integer oid) {
        List<Orderitem> orderitems = orderitemMapper.selectByOid2(oid);
        if (orderitems.isEmpty()){
            return null;
        }
        return  orderitems;
    }

    @Override
    public int addNewOrderItem(Integer oid, Integer bid, Integer num, BigDecimal bprice, BigDecimal total) {
        Orderitem orderitem = new Orderitem();
        orderitem.setOid(oid);
        orderitem.setBid(bid);
        orderitem.setNum(num);
        orderitem.setPrice(bprice);
        orderitem.setSubtotal(total);
        int i = orderitemMapper.insertSelective(orderitem);
        if (i>0){
            return 200;
        }
        return 400;
    }

    @Override
    public Orderitem findOrderItemByOiid(Integer oiid) {
        return orderitemMapper.selectByOiid(oiid);
    }

    @Override
    public int deleteOrderItemByOid(Integer oid) {
        OrderitemExample orderitemExample = new OrderitemExample();
        OrderitemExample.Criteria criteria = orderitemExample.createCriteria();
        criteria.andOidEqualTo(oid);
        if (orderitemMapper.deleteByExample(orderitemExample)>0){
            return 200;
        }
        return 400;
    }
}
